package com.java.controller;

import com.alibaba.fastjson.JSONArray;
import com.java.pojo.Page;
import com.java.pojo.Repository;
import com.java.pojo.User;
import com.java.service.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@Controller
@RequestMapping("/Repository")
public class RepositoryController {

    @Autowired
    private RepositoryService repositoryService;




    @RequestMapping("/findAllRepository")
    public String findAllUser(@RequestParam(value = "start",required = false) Integer start,
                              @RequestParam(value = "rname",required = false)String rname ,
                              Model model){
        if(rname==null ||"".equals(rname)){
            rname="";
        }
        if(start==null ||start.equals("")){
            start=1;
        }
        System.out.println(start+"+"+rname);
        /*计算总页数*/
        int totalCount = repositoryService.getCount(rname);
        int i = totalCount % 4 ==0? totalCount/4 :(totalCount/4)+1;
        /*如果最后一页大于总页数,就让其等于总页数,html按钮最后一页
         * 如果前一页到0的时候,把他变成1*/
        if(start>=i){
            start=i;
        }else if(start<=1){
            start=1;
        }
        Page<Repository> page =repositoryService.getPageByRepository(start,rname);
        System.out.println("page:"+page);

        model.addAttribute("page",page);
        model.addAttribute("rname",rname);
        return "Repository/findAllRepository";
    }



    //添加
    @RequestMapping("/addR")
    public String addR(){
        return "Repository/addRe";
    }


    @RequestMapping(value = "/ADDRE",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String ADDRE(Repository repository){
        int i = repositoryService.addRepository(repository);
        if(i>0){
            return "<script>alert('ADD successful');location.href='/Repository/findAllRepository';</script>";
        } else{
            return "<script>alert('仓库名称重复! ADD 失败!');location.href='/Repository/findAllRepository';</script>";
        }
    }

    @RequestMapping(value = "/DELRepository",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String DELRepository(@RequestParam("rid") Integer rid){
        int i = repositoryService.delR(rid);
        if(i>0){
            return "<script>alert('DEL successful');location.href='/Repository/findAllRepository';</script>";
        } else{
            return "<script>alert('DEL 失败!');location.href='/Repository/findAllRepository';</script>";
        }
    }

    //ajax
    @RequestMapping("/ADDRES2")
    @ResponseBody
    public Object log2(@RequestParam("name") String name){
        HashMap<String,String> map=new HashMap<>();//集合
        Repository reByName = repositoryService.getREByName(name);
        System.out.println(name+":"+reByName);
        if(reByName!=null){
            map.put("userCode","exist");
        }else{
            map.put("userCode","no");
        }
        //将map转为json
        return JSONArray.toJSONString(map);
    }



}
